package com.heyqing.topic.AddBinary_0067;

import java.util.Scanner;

/**
 * ClassName:AddBinary
 * Package:com.heyqing.topic.AddBinary_0067
 * Description:
 *
 * @Date:2024/9/10
 * @Author:Heyqing
 */
public class AddBinary {
    public String addBinary(String a, String b) {
        int flag = 0;
        int dVal = Math.abs(a.length() - b.length());
        StringBuffer sbDVal = new StringBuffer();
        for (int i = 0; i < dVal; i++) {
            sbDVal.append("0");
        }
        String dvs = sbDVal.toString();
        if (a.length() > b.length()) {
            b = dvs + b;
        } else {
            a = dvs + a;
        }
        StringBuffer res = new StringBuffer();
        for (int i = a.length() - 1; i >= 0; i--) {
            int sumTemp = Integer.parseInt(String.valueOf(a.charAt(i))) + Integer.parseInt(String.valueOf(b.charAt(i))) + flag;
            res.append(sumTemp % 2);
            flag = sumTemp / 2;
        }
        if (flag == 1){
            res.append(flag);
        }
        return res.reverse().toString();
    }

    public static void start() {
        AddBinary addBinary = new AddBinary();
        Scanner sc = new Scanner(System.in);
        System.out.println("a:");
        String a = sc.nextLine();
        System.out.println("b:");
        String b = sc.nextLine();
        String s = addBinary.addBinary(a, b);
        System.out.println("结果：" + s);
    }
}
